package Gui;

import io.DbFuncs;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.MenuItem;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;
import org.eclipse.swt.widgets.Text;

import track.playlistEntry;
import auth.User;
import auth.UserFuncs;

public class Search {
	public static int numberOfSearchResults = 14;
	public static int currEntry = -1;
	public static playlistEntry[] pEntries = null;
	public Display searchDisplay;
	private Shell searchShell;
	public Search(Shell shell, final User mUser, final Main main){
		this.searchDisplay = shell.getDisplay();
		this.searchShell = new Shell (searchDisplay);
		main.addShell(searchShell);
		invokeSearch(shell, searchShell, searchDisplay, mUser, main);
	}
	public static void skipCell(Composite form, String sBlank){
		Label mDummy = new Label(form, SWT.NONE);
		mDummy.setLayoutData(new GridData(GridData.BEGINNING,GridData.CENTER,false,true));
		mDummy.setText(sBlank);
	}
	public static void updateTable(Table table, Button gotoBt, Button updateBt){
		int i;
		gotoBt.setEnabled(false);
		updateBt.setEnabled(false);
		table.removeAll();
		for(i=0;i<numberOfSearchResults;i++){
			String[] tableEntry = {pEntries[i].getArtist().getName(), pEntries[i].getSong().getName(), pEntries[i].getAlbum().getName()};
			TableItem TI = new TableItem(table, 0);
			TI.setText(tableEntry);
		}
	}
	public static void invokeSearch(Shell shell, final Shell searchShell, Display searchDisplay, final User mUser, final Main main){
		final Shell passShellForward = shell;
		searchShell.setText("Search");
		searchShell.setLocation(shell.getLocation().x + 50, shell.getLocation().y + 50);
		
		Menu topMenu = new Menu(searchShell,SWT.BAR);
		searchShell.setMenuBar(topMenu);
		MenuItem gotoPlaylist = new MenuItem(topMenu,SWT.CASCADE);
		MenuItem closeBt = new MenuItem(topMenu,SWT.CASCADE);
		gotoPlaylist.setText("Go to Playlist");
		closeBt.setText("Close search");
		
		Composite form = new Composite(searchShell,SWT.BORDER);      
		form.setLayout(new GridLayout(2,false));
		int i, rowsToSkip=1;
		for(i=0;i<rowsToSkip*2;i++)
			skipCell(form, "");
		
		final Text searchInput = new Text(form,SWT.SINGLE | SWT.BORDER);
		searchInput.setLayoutData(new GridData(GridData.FILL,GridData.CENTER,true,false));
		
		Button searchBt = new Button(form,0);
		searchBt.setText("Search");
		searchBt.setLayoutData(new GridData(GridData.BEGINNING,GridData.CENTER,false,false));
		
		final Label searchRes = new Label (form, SWT.NONE);
		searchRes.setLayoutData(new GridData(GridData.CENTER,GridData.CENTER,false,false));
		searchRes.setText ("Search Results are shown in the table above");
		
		skipCell(form, "");
		
		final Label addiText = new Label (form, SWT.NONE);
		addiText.setLayoutData(new GridData(GridData.CENTER,GridData.CENTER,false,false));
		addiText.setText ("You may click a result for more features");
		
		skipCell(form, "");
		
		final Button gotoBt = new Button(form,0);
		gotoBt.setText("See selection features");
		gotoBt.setLayoutData(new GridData(GridData.CENTER,GridData.CENTER,false,false));
		
		gotoBt.setEnabled(false);
		
		final Table resultsTable = new Table(searchShell, SWT.BORDER | SWT.SINGLE | SWT.FULL_SELECTION);

		TableColumn artistColumn  = new TableColumn(resultsTable,SWT.NONE);
		artistColumn.setText("Artist name");
		TableColumn songColumn = new TableColumn(resultsTable,SWT.NONE);
		songColumn.setText("Song name");
		TableColumn albumColumn = new TableColumn(resultsTable,SWT.NONE);
		albumColumn.setText("Album name");

		resultsTable.setHeaderVisible(true);
		double colSize = (double)1/resultsTable.getColumnCount();
		form.setBackgroundImage(new Image(searchDisplay, "bgs/bg5.jpg"));
		int tableFullWidth = searchShell.getSize().y - resultsTable.getBorderWidth()*2;
		for(TableColumn TC: resultsTable.getColumns()){
			TC.setWidth((int)(tableFullWidth * colSize));
		}

		searchShell.setLayout(new FormLayout());

		form.pack();

		FormData topData = new FormData();
		FormData bottomData = new FormData();
		
		topData.left = new FormAttachment(0);
		topData.right = new FormAttachment(100);
		topData.top = new FormAttachment(0);
		topData.bottom = new FormAttachment(100);

		bottomData.left = new FormAttachment(0);
		bottomData.right = new FormAttachment(100);
		bottomData.top = new FormAttachment(70);
		bottomData.bottom = new FormAttachment(100);

		resultsTable.setLayoutData(topData);
		form.setLayoutData(bottomData);
		
		gotoPlaylist.addSelectionListener(
				new SelectionAdapter() {
					public void widgetSelected(SelectionEvent e) {
						main.removeShell(searchShell);
						if(main.isPlaylistActive()){
							main.closeActiveShells();
						}
						main.setSearchNotActive();
						Playlist playlist = new Playlist(searchShell, passShellForward, mUser, null, main);
					}
				}
		);
		
		searchBt.addSelectionListener(
				new SelectionAdapter() {					
					public void widgetSelected(SelectionEvent e) {
						int i;
						gotoBt.setEnabled(false);
						resultsTable.removeAll();
						pEntries = DbFuncs.getResult(searchInput.getText(), numberOfSearchResults);
						if(pEntries != null){
							for(i=0;i<pEntries.length;i++){
								String[] tableEntry = {pEntries[i].getArtist().getName(), pEntries[i].getSong().getName(), pEntries[i].getAlbum().getName()};
								TableItem TI = new TableItem(resultsTable, 0);
								TI.setText(tableEntry);
							}
						}
					}
				}
		);
		
		gotoBt.addSelectionListener(
				new SelectionAdapter() {					
					public void widgetSelected(SelectionEvent e) {
						if(main.isPlaylistActive()){
							main.closeActiveShells();
						}
						main.setSearchNotActive();
						Playlist playlist = new Playlist(searchShell, passShellForward, mUser, pEntries[currEntry], main);
					}
				}
		);
		
		Listener listener = new Listener () {
			public void handleEvent (Event e) {
				switch (e.type) {
				case SWT.Close:
					main.removeShell(searchShell);
					main.setSearchNotActive();
					searchShell.dispose();
					break;
				default:
		        }	
		   }
		};
		searchShell.addListener (SWT.Close, listener);
		
		closeBt.addSelectionListener(
				new SelectionAdapter() {					
					public void widgetSelected(SelectionEvent e) {
						main.removeShell(searchShell);
						main.setSearchNotActive();
						searchShell.dispose();
					}
				}
		);
		
		resultsTable.addSelectionListener(
				new SelectionAdapter() {
					public void widgetSelected(SelectionEvent e) {
						gotoBt.setEnabled(true);
						currEntry = resultsTable.getSelectionIndex();
					}
					public void widgetDefaultSelected(SelectionEvent e) {
						currEntry = resultsTable.getSelectionIndex();
						if(main.isPlaylistActive()){
							main.closeActiveShells();
						}
						main.setSearchNotActive();
						Playlist playlist = new Playlist(searchShell, passShellForward, mUser, pEntries[currEntry], main);
					}
				}
		);
		resultsTable.setSelection(0);
		
		searchShell.pack();
		searchShell.open();

		while (!searchShell.isDisposed()) {
			if (!searchDisplay.readAndDispatch())
				searchDisplay.sleep();
		}
	}
}
